﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Ria;
using System.Web.Ria.ApplicationServices;
using System.Web.Security;
using System.Security.Principal;
using System.Web.DomainServices;
using System.ComponentModel;

namespace AuthenticationService.Web
{
    [EnableClientAccess]
    public class AuthenticationService : AuthenticationBase<User> 
    {
        protected override void OnError(DomainServiceErrorInfo errorInfo)
        {
            base.OnError(errorInfo);
        }
        public override void Initialize(DomainServiceContext context)
        {
            base.Initialize(context);
        }

        [RequiresAuthentication]
        [RequiresRole("Admin")]
        public IEnumerable<User> GetAllUsers()
        {
            return Membership.GetAllUsers().Cast<MembershipUser>().Select(mu => this.GetUserForMembershipUser(mu));
        }

        private User GetUserForMembershipUser(MembershipUser membershipUser)
        {
            return this.GetAuthenticatedUser(
                new GenericPrincipal(new GenericIdentity(membershipUser.UserName), new string[0]));
        }
    }

    public class User : UserBase, INotifyPropertyChanged
    {
        //public new string Name
        //{
        //    get
        //    {
        //        return base.Name;
        //    }
        //    set
        //    {
        //        base.Name = value;
        //        NotifyPropertyChanged("Name");
        //    }
        //}

        //public new IEnumerable<string> Roles
        //{
        //    get
        //    {
        //        return base.Roles;
        //    }
        //    set
        //    {
        //        base.Roles = value;
        //        NotifyPropertyChanged("Roles");
        //    }
        //}

        private string _fullName;
        public string FullName
        {
            get
            {
                return _fullName;
            }
            set
            {
                _fullName = value;
                NotifyPropertyChanged("FullName");
            }
        }

        private string _phoneNumber;
        public string PhoneNumber
        {
            get
            {
                return _phoneNumber;
            }
            set
            {
                _phoneNumber = value;
                NotifyPropertyChanged("PhoneNumber");
            }
        }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        private void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        #endregion
    }
}
